<?php
/**
 * Created by PhpStorm.
 * User: 44766
 * Date: 2018-10-08
 * Time: 19:22
 */

namespace App\Http\Controllers\api\traits;


use Illuminate\Support\Collection;
use Request;

trait ModelController
{


    /**
     * @param \Eloquent $model
     * @param $where
     */
    public function where(&$model, $where)
    {
        foreach ($where as $field => $value) {
            if (!is_string($field)) {
                continue;
            }
            if (is_array($value)) {
                switch ($value[0]) {
                    case "in":
                        $model = $model->whereIn($field, $value[1]);
                        break;
                    case "not_in":
                        $model = $model->whereNotIn($field, $value[1]);
                        break;
                    default:
                        $model = $model->where($field, $value[0], $value[1]);
                }
            } else {
                $model = $model->where($field, $value);
            }
        }
    }

    /**
     * @param \Eloquent $model
     * @param $with
     */
    public function with(&$model, $with)
    {
        foreach ($with as $relation) {
            $model = $model->with($relation);
        }
    }

    /**
     * @param \Eloquent $model
     * @param $has
     */
    public function has(&$model, $has)
    {
        foreach ($has as $relation => $condition) {
            $model = $model->whereHas($relation, function ($query) use ($condition) {
                if ($condition['where']) {
                    $this->where($query, $condition['where']);
                }
            });
        }
    }

    /**
     * @param \Eloquent $model
     * @param $order
     * @param string $sort
     */
    public function order(&$model, $order, $sort = 'ase')
    {
        $model = $model->orderBy($order, $sort);
    }

    /**
     * @param \Eloquent $model
     * @param $limit
     */
    public function limit(&$model, $limit)
    {
        $model = $model->limit($limit);
    }

    /**
     * @param \Eloquent $model
     * @param array $fields
     */
    public function select(&$model, $fields = [])
    {
        $model = $model->select($fields);
    }


    public function renderList(&$model)
    {
        if (Request::get('where')) {
            $this->where($model, Request::get('where'));
        }

        if (Request::get('has')) {
            $this->has($model, Request::get('has'));
        }

        if (Request::get('with')) {
            $this->with($model, Request::get('with'));
        }

        if (Request::get('order')) {
            $this->order($model, Request::get('order'), Request::get('sort', 'ase'));
        }

        $this->limit($model, Request::get('limit', 100));

        if (Request::get('field')) {
            $this->select($model, Request::get('field'));
        }

        return $this;
    }

    /**
     * @param \Eloquent $model
     * @return \Illuminate\Http\JsonResponse
     */
    public function autoPage(&$model)
    {
        if (Request::get('paging')) {
            return $this->package($model->paginate());
        } elseif (Request::get('group')) {
            return $this->packageGroup($model->get(), Request::get('group'));
        } else {
            return $this->packageList($model->get());
        }
    }

    public function packageGroup($model, $groupName)
    {
        $list = [];
        foreach ($model as $item) {
            if($groupName instanceof \Closure){
                $key=$groupName($item);
            }else{
                $key=$item->$groupName;
            }
            if (!isset($list[$key])) {
                $list[$key] = [
                    "groupName" => $key,
                    "children"  => []
                ];
            }
            $list[$key]['children'][] = $item;
        }
        ksort($list);
        $list = array_values($list);
        return $this->packageList($list);
    }
}